package com.xmkj.bearuser.toutiao;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.Entity.ArticalEntity;
import com.common.listener.OnOnceClickListener;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.method.DaShangArticalMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.widget.dialog.BaseBottomDialog;
import com.common.widget.imageview.image.ImageLoaderUtils;
import com.xmkj.bearuser.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AticleActivity extends BaseMvpActivity {
    public static final String INFOBEAN = "infobena";
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_dashang)
    TextView tvDashang;
    private TextView tvIntrue;
    private ImageView ivReduce;
    private ImageView ivAdd;
    private EditText etSum;
    private ImageView ivClose;
    private int currentnum = 1;
    private ArticalEntity.ListsBean bean;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_article;
    }

    @Override
    protected void onViewCreated() {
        bean = (ArticalEntity.ListsBean) getIntent().getSerializableExtra(INFOBEAN);
        if (bean != null) {
            setNavigationBack(bean.title);
            if(EmptyUtils.isNotEmpty(bean.pic)){
                ImageLoaderUtils.display(ivImg, bean.pic);

            }
            tvContent.setText(bean.content);
            tvTitle.setText(bean.title);
        }
    }

    @Override
    protected void doLogicFunc() {
        attachClickListener(tvDashang);
    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == tvDashang.getId()) {
            showButton();
        }
    }

    private BaseBottomDialog dialogFragment;

    private void showButton() {
        dialogFragment = BaseBottomDialog.newInstance(R.layout.dashang);
        dialogFragment.show(((BaseMvpActivity) context).getSupportFragmentManager(), "shareDialog");
        ((BaseMvpActivity) context).getSupportFragmentManager().executePendingTransactions();
        tvIntrue = (TextView) dialogFragment.getDialog().findViewById(R.id.tv_intrue);
        ivReduce = (ImageView) dialogFragment.getDialog().findViewById(R.id.iv_reduce);
        ivAdd = (ImageView) dialogFragment.getDialog().findViewById(R.id.iv_add);
        etSum = (EditText) dialogFragment.getDialog().findViewById(R.id.et_sum);
        ivClose = dialogFragment.getDialog().findViewById(R.id.iv_close);
        ivClose.setOnClickListener(new OnOnceClickListener() {
            @Override
            public void onOnceClick(View v) {
                dialogFragment.dismiss();
            }
        });
        ivReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (currentnum > 1) {
                    currentnum--;
                }

                etSum.setText(currentnum + "");
            }
        });
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                currentnum++;
                etSum.setText(currentnum + "");
            }
        });
        tvIntrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                daShang();
            }
        });


    }

    private void daShang() {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();

                showToastMsg("成功");

//                gotoHttpRep();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        DaShangArticalMethods.getInstance().getScore(subscriber, uid, hashid,  Integer.valueOf(getEditTextStr(etSum)) ,bean.articleid+"");
        rxManager.add(subscriber);
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("头条文章");

    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
